﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;

namespace NursingFormSystem.CVP
{
    public partial class CVPPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        private int formId;
        DateTime Start;
        DateTime End;
        public CVPPrints(PatientInfo patientiInfo, int formId, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            this.PatientInfo = patientiInfo;
            this.formId = formId;
            Start = StartDate;
            End = EndDate;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\中心静脉压监测表.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, formId, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    CVPS ps = XmlUtil.XMLToObject<CVPS>(record.NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["jianceriqi"] = ps.JianCeRiQi;
                    newRow["shijian"] = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);
                    newRow["zhongxinjingmaiya"] = ps.ZhongXinJingMaiYa;
                    newRow["PatientName"] = record.PatientName;
                    newRow["SEX"] = PatientInfo.SEX;
                    newRow["AGE"] = PatientInfo.AGE;
                    newRow["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;
                    newRow["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;
                    newRow["BED_NO"] = PatientInfo.BED_NO;
                    newRow["PatientId"] = PatientInfo.PATIENT_ID;
                    dt.Rows.Add(newRow);
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("jianceriqi", typeof(string));
            dt.Columns.Add("shijian", typeof(string));
            dt.Columns.Add("zhongxinjingmaiya", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void CVPPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}